package com.bw.android.network;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Author: conghao
 * Date: 2023/9/12 11:29
 * Description: RequestClient
 */
public class RequestClient {
    private volatile static OkHttpClient mHttpClient;
    private RequestClient() {
        throw new IllegalAccessError("Can not access this method");
    }

    /**
     * 获取OkHttpClient单例
     * @return mkHttpClient
     */
    private static OkHttpClient buildClient() {
        if(mHttpClient == null) {
            synchronized (RequestClient.class) {
                if(mHttpClient == null) {
                    mHttpClient = new OkHttpClient.Builder()
                            .connectTimeout(10, TimeUnit.SECONDS)
                            .readTimeout(30, TimeUnit.SECONDS)
                            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))
                            .build();
                }
            }
        }
        return mHttpClient;
    }
    public static ApiService buildService() {
        return new Retrofit.Builder()
                .baseUrl(ApiService.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(buildClient())
                .build()
                .create(ApiService.class);
    }

}
